package com.dugan.cekilis;

import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.AdapterView.*;
import java.util.*;

import android.view.View.OnClickListener;
import android.widget.SeekBar.*;

public class MainActivity extends Activity 
{
	ArrayList<String> inputs;
	
	ListAdapter adapter;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

		inputs = new ArrayList<>();
		
		//adapter = new CustomArrayAdapter(this, android.R.layout.simple_list_item_1, inputs);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, inputs);
        //adapter = new CustomBaseAdapter(this, (String[])inputs);
        
		final ListView listView = (ListView) findViewById(R.id.mainListView1);
		//listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		listView.setAdapter(adapter);
	

		listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> l, View v, int position, long id)
				{
					//DeleteDialogFragment f = new DeleteDialogFragment();				
					//f.show(getFragmentManager(), "mydialog");
					
					String s = (String) l.getItemAtPosition(position);
					Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
					
				}
			});

		final EditText editText = (EditText) findViewById(R.id.mainEditText1);

		Button ekleButton = (Button) findViewById(R.id.mainButton1);

		ekleButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View p1)
				{

					if ("".equals(editText.getText()))
					{
						return;
					}

					//ListAdapter ad = listView.getAdapter();
					inputs.add(editText.getText().toString());
					editText.setText("");
					//ListAdapter adap = new ArrayAdapter<String>(p1.getContext(), android.R.layout.simple_list_item_1, inputs);
					//listView.setAdapter(ad);

				}
			});
			
		
		Button cekButton = (Button) findViewById(R.id.mainButton2);

		//final Random random = new Random();
		cekButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View p1)
				{
					//ArrayList<String> peers = Raffle.setPairsAli(inputs);
					ArrayList<String> peers = Raffle.setPairsNazim(inputs);
					
					for(int i=0;i<peers.size();++i){
						Log.e("inputs", inputs.get(i));
						Log.e("peers", peers.get(i));
						//new AsyncEmailTask().execute(inputs.get(i),peers.get(i));
					}

				}
			});
			
		
		CheckBox cb = (CheckBox) findViewById(R.id.rowCheckBox1);
			
	}	
		


    private class AsyncEmailTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            try {   
				GmailSender sender = new GmailSender("cekilisapp@gmail.com", "Duga1002");
				sender.sendMail(params[0],   
								params[1],   
								"cekilisapp@gmail.com",   
								"alidugan@gmail.com");   
			} catch (Exception e) {   
				Log.e("SendMail", e.getMessage(), e);   
			} 
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected void onProgressUpdate(Void... values) {
        }
    }	
	
	
	class DeleteDialogFragment extends DialogFragment
	{
		@Override
		public Dialog onCreateDialog(Bundle savedInstanceState)
		{
			AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
			builder.setTitle("Sil");
			builder.setMessage("Katılımcı silinsin mi?");
			builder.setPositiveButton("Evet", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface p1, int p2)
					{
						// TODO: Implement this method
						getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
					}
				});

			builder.setNegativeButton("İptal", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface p1, int p2)
					{
						// TODO: Implement this method
					}
				});

			return builder.create();
		}
		
		
	}
			
			

    
}
